<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { luckDrawApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  const formLabelWidth = '100px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    title:'',
    dialogFormVisible:false,
    disabled:false,
    poolList:[] as Array<any>,
  })
  interface formQuert {
    extract_pool_id:number
    pool_name:string
    pool_id:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    pool_name: [{ required: true, message: "请输入礼物池名称", trigger: "blur" }],
    pool_id: [{ required: true, message: "请选择关联礼物池", trigger: "blur" }],
  }
  const getpoolList = async()=>{
    const res = await luckDrawApi.wishWheelPoolList({
      pool_type:1,
    })
    const { data=[] } = res || {data:[]}
    action.poolList = data
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const { extract_pool_id } = form
      const loading = Toastloading()
      let submitSet = false
      if(extract_pool_id) {
        await luckDrawApi.wishWheelEditExtractPool(form).then(()=>{
          ToastSuccess('编辑抽取礼物池成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }else {
        await luckDrawApi.wishWheelAddExtractPool(form).then(()=>{
          ToastSuccess('新增抽取礼物池成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        $emit('updateList')
        closeDialog()
      }
    })
  }
  const openDialog = async(query:any = {})=> {
    const {
      id='',pool_id='',pool_name='',
    } = query
    let obj = {
      pool_id,
      pool_name
    } as any
    if(id) {
      obj['extract_pool_id'] = id
      action.title = '编辑抽取礼物池：' + pool_name
      action.disabled = true
    } else {
      action.title = '新增抽取礼物池'
      action.disabled = false
    }
    Object.assign(form,obj)
    const loading = Toastloading()
    if(action.poolList.length == 0) await getpoolList()
    loading.close()
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="5vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="false"
      :show-close="false"
      :close-on-press-escape="false"
      width="35%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="礼物池名称" prop="pool_name">
          <el-input v-model="form.pool_name" placeholder="请输入礼物池名称"  />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="关联礼物池" prop="pool_id">
            <el-select :disabled="action.disabled" v-model="form.pool_id" filterable placeholder="请选择关联礼物池">
            <el-option
              v-for="item in action.poolList"
              :label="item.pool_name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
